package com.zhw.andfix;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.Unbinder;
import cn.droidlover.xdroidmvp.XDroidConf;
import cn.droidlover.xdroidmvp.kit.KnifeKit;
import cn.droidlover.xdroidmvp.mvp.IPresent;
import cn.droidlover.xdroidmvp.mvp.IView;
import cn.droidlover.xdroidmvp.mvp.VDelegate;
import cn.droidlover.xdroidmvp.mvp.VDelegateBase;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.router.Router;

/**
 * Created by Administrator on 2018/3/6.
 */

public abstract class BaseTitlebarActivity<P extends IPresent> extends RxAppCompatActivity implements IView<P>, View.OnClickListener {
    private LinearLayout toolbar;
    private ImageView tvBack;
    private TextView ivMore;
    private TextView tvTitle;
    private TextView tvRedot;
    protected AutoCompleteTextView etKey;
    protected static final String TAG = "BaseTitlebarActivity";
//    private LoadingDialog loadingDialog;

    private VDelegate vDelegate;
    private P p;
    protected Activity context;

    private RxPermissions rxPermissions;

    private Unbinder unbinder;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        if (this instanceof MainActivity) {

        }
        if (getLayoutId() > 0) {
            setContentView(getLayoutId());
            bindUI(null);
            bindEvent();

            initTitleBar();
        }
        initData(savedInstanceState);
//        if (useEventBus()) {
//            EventBus.getDefault().register(this);
//        }
//        if (!UtilNetwork.isConnected()){
//            showShort("网络没有连接");
//        } else {
//            if (!UtilNetwork.isAvailableByPing()){
//                showShort("网络连接异常");
//            }
//        }
    }

    @Override
    public void bindUI(View rootView) {
        unbinder = KnifeKit.bind(this);
    }

    protected VDelegate getvDelegate() {
        if (vDelegate == null) {
            vDelegate = VDelegateBase.create(context);
        }
        return vDelegate;
    }

    protected P getP() {
        if (p == null) {
            p = newP();
            if (p != null) {
                p.attachV(this);
            }
        }
        return p;
    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    protected void onResume() {
        super.onResume();
        getvDelegate().resume();
    }


    @Override
    protected void onPause() {
        super.onPause();
        getvDelegate().pause();
    }

    @Override
    public boolean useEventBus() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (useEventBus()) {
//            BusProvider.getBus().unregister(this);
//            EventBus.getDefault().unregister(this);
        }
        if (getP() != null) {
            getP().detachV();
        }
        getvDelegate().destory();
        p = null;
        vDelegate = null;
        unbinder.unbind();
        unbinder = null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (getOptionsMenuId() > 0) {
            getMenuInflater().inflate(getOptionsMenuId(), menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    protected RxPermissions getRxPermissions() {
        rxPermissions = new RxPermissions(this);
        rxPermissions.setLogging(XDroidConf.DEV);
        return rxPermissions;
    }

    @Override
    public int getOptionsMenuId() {
        return 0;
    }

    @Override
    public void bindEvent() {

    }


    private void initTitleBar() {
        toolbar = (LinearLayout) findViewById(R.id.titlebar);
        if (toolbar != null) {
            tvBack = (ImageView) findViewById(R.id.iv_back);
            ivMore = (TextView) findViewById(R.id.iv_more);
            tvTitle = (TextView) findViewById(R.id.tv_title);
            tvRedot = (TextView) findViewById(R.id.tv_redot);
            etKey = (AutoCompleteTextView) findViewById(R.id.et_key);
            tvBack.setVisibility(showBack() ? View.VISIBLE : View.INVISIBLE);
            ivMore.setVisibility(showMore() ? View.VISIBLE : View.INVISIBLE);
            if (tvRedot != null) {
                tvRedot.setVisibility(showRedot() ? View.VISIBLE : View.INVISIBLE);
            }
            toolbar.setBackgroundColor(ContextCompat.getColor(this, getToolbarBgColorId()));
            initTitleBarListener();
        }
    }
    private void initTitleBarListener() {
        tvBack.setOnClickListener(this);
        ivMore.setOnClickListener(this);
    }
    public void setTitle(int resId) {
        if (tvTitle != null)
            tvTitle.setText(resId);
    }
    public void setTitle(String title) {
        if (tvTitle != null)
            tvTitle.setText(title);
    }
    public void setRedotText(String text) {
        if (tvRedot != null && !TextUtils.isEmpty(text)) {
            tvRedot.setText(text);
        }
    }
    public void setRedotText(int textId) {
        if (tvRedot != null) {
            tvRedot.setText(textId);
        }
    }

    public void setMoreImageResource(int resId){
        if (ivMore == null)
            return;
        ivMore.setBackgroundResource(resId);
    }
    public void setMoreBackground(StateListDrawable stateListDrawable) {
        if (ivMore == null)
            return;
        ivMore.setBackgroundDrawable(stateListDrawable);//给View设置selector
    }

    public void setMoreText(int resId){
        if (ivMore != null){
            ivMore.setText(resId);
        }
    }
    public void setMoreText(String msg){
        if (ivMore != null){
            ivMore.setText(msg);
        }
    }

    public void setTitlebarBg(int colorId){
        if (toolbar != null){
            toolbar.setBackgroundColor(ContextCompat.getColor(context, colorId));
        }
    }
    protected void setBackVisibility(int visibility) {
        if (tvBack != null) {
            tvBack.setVisibility(visibility);
        }
    }
    protected void setMoreVisibility(int visibility) {
        if (ivMore != null) {
            ivMore.setVisibility(visibility);
        }
    }
    protected void setToolbarVisibility(int visibility) {
        if (toolbar != null) {
            toolbar.setVisibility(visibility);
        }
    }
    public boolean showBack() {
        return true;
    }
    public boolean showMore() {
        return false;
    }

    public boolean showRedot() {
        return false;
    }

    public int getToolbarBgColorId(){
        return R.color.colorPrimary;
    }
    protected void initView() {
    }
    protected void initListener() {
    }
    protected void initData() {
    }
    protected void loadCompleted() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                doBack();
                break;
            case R.id.iv_more:
                doMore();
                break;
        }
    }
    public void doMore() {
    }
    public void doBack() {
        finish();
    }

    public void showShort(int resId){
    }
    public void showShort(String msg){
    }
    public void showLong(int resId){
    }
    public void showLong(String msg){
    }

    /*public void showLoadingDialog(@Nullable String msg) {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.newInstance(msg);
        }
        try {
            getSupportFragmentManager().executePendingTransactions();
            if (!loadingDialog.isAdded()&&!loadingDialog.isRemoving()&&!loadingDialog.isVisible()) {
                loadingDialog.setMessage(msg);
                loadingDialog.show(getSupportFragmentManager(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    /*public void showLoadingDialog(int stringId) {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.newInstance(stringId);
        }
        try {
            getSupportFragmentManager().executePendingTransactions();
            if (!loadingDialog.isAdded()&&!loadingDialog.isRemoving()&&!loadingDialog.isVisible()) {
                loadingDialog.setStringId(stringId);
                loadingDialog.show(getSupportFragmentManager(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    /*public void dismissLoadingDialog() {
        if (loadingDialog != null) {
            try{
                loadingDialog.dismiss();
            }catch (Exception e){e.printStackTrace();}
        }
    }*/

    public void showNetError(NetError netError){
        showShort(netError.getMessage());
    }

    public void jump2Activity(Class<?> cls, Bundle bundle){
        Router.newIntent(this).to(cls)
                .data(bundle).launch();
    }
    public void jump2Activity(Class<?> cls, Bundle bundle, int requestCode){
        Router.newIntent(this).to(cls)
                .requestCode(requestCode)
                .data(bundle).launch();
    }
}
